<?php
namespace app\admin\controller;

use app\common\business\Goods as GoodsBus;

class Goods extends AdminBase
{
    public function index()
    {
        $data = [];
        $title = input('param.title','','trim');
        $time = input('param.time','','trim');

        if (!empty($title)) {
            $data['title'] = $title;
        }

        if (!empty($time)) {
            $data['time'] = $time;
        }

        $goods = (new GoodsBus())->getLists($data, 5);

        return view("",[
            'goods' => $goods,
            'title' => $title,
            'time' => $time,
        ]);
    }

    public function add()
    {
        return view();
    }

    public function save()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'),'参数不合法');
        }

        $data = input('param.');

        //csrf验证
        $check = $this->request->checkToken($data['__token__']);
        if (!$check) {
            return show(config('status.error'),'非法请求！！！');
        }

        //TODO validate验证
        $validate = new \app\admin\validate\Goods();

        $data['category_path_id'] = $data['category_id'];
        $result = explode(',',$data['category_path_id']);
        $data['category_id'] = end($result);

        try {
            $res = (new GoodsBus())->insertData($data); //核心操作
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if($res) {
            return show(config('status.success'),'商品增加成功');
        }

        return show(config('status.error'),'商品增加失败');

    }


    /**
     * 商品排序
     * @return \think\response\Json
     */
    public function listorder()
    {
        $id = input('param.id', 0, 'intval');
        $listorder = input('param.listorder', 0, 'intval');

        $data = [
            'id' => $id,
            'listorder' => $listorder,
        ];

        $validate = new \app\admin\validate\Goods();
        if (!$validate->scene('goods_listorder')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $res = (new GoodsBus())->listorder($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return show(config('status.success'),'排序成功');
        }

        return show(config('status.error'),'排序失败');
    }

    /**
     * 修改商品状态
     * @return \think\response\Json
     */
    public function status()
    {
        $id = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');

        $data = [
            'id' => $id,
            'status' => $status,
        ];

        $validate = new \app\admin\validate\Goods();
        if (!$validate->scene('goods_status')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $res = (new GoodsBus())->status($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res) {
            return show(config('status.success'),'状态更新成功');
        }

        return show(config('status.error'),'状态更新失败');
    }

    /**
     * 是否推荐
     * @return \think\response\Json
     */
    public function recommend()
    {
        $id = input('param.id', 0, 'intval');
        $recommend = input('param.recommend', 0, 'intval');

        $data = [
            'id' => $id,
            'is_index_recommend' => $recommend,
        ];

        $validate = new \app\admin\validate\Goods();
        if (!$validate->scene('goods_recommend')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $res = (new GoodsBus())->recommend($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res) {
            return show(config('status.success'),'状态更新成功');
        }

        return show(config('status.error'),'状态更新失败');
    }
}